<!--
 * @Description: 物料维护
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-04-26 20:47:16
 * @FilePath: /yuxiuproweb/src/views/process/vm/mods/maintenance/index.vue
-->
<template>
  <el-drawer title="物料维护" size="70%" :visible.sync="shows">
    <div class="app-container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['md:fabric:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['md:fabric:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['md:fabric:export']"
            >导出</el-button
          >
        </el-col>
      </el-row>

      <el-table
        v-loading="loading"
        :data="fabricList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="类型" align="center" prop="type">
          <template slot-scope="{ row }">
            {{ row.type === "0" ? "面料" : "辅料" }}
          </template>
        </el-table-column>
        <el-table-column label="用料环节" align="center" prop="usageProcess">
          <template slot-scope="{ row }">
            {{ getTypeName(USAGE_PROCESS, row.usageProcess) }}
          </template>
        </el-table-column>
        <el-table-column label="位置" align="center" prop="part" />
        <el-table-column
          label="是否自动拆单"
          align="center"
          prop="isAutomaticOrderSplitting"
          min-width="120"
        >
          <template slot-scope="{ row: { isAutomaticOrderSplitting } }">
            {{
              isAutomaticOrderSplitting === "Y"
                ? "是"
                : isAutomaticOrderSplitting === "N"
                ? "否"
                : ""
            }}
          </template>
        </el-table-column>
        <el-table-column label="宽度比例" align="center" prop="widthRatio" />
        <el-table-column label="宽度附加" align="center" prop="widthAddition" />
        <el-table-column label="高度比例" align="center" prop="heightRatio" />
        <el-table-column
          label="高度附加"
          align="center"
          prop="heightAddition"
        />
        <el-table-column label="每N件" align="center" prop="unitQuantity" />
        <el-table-column label="使用数量" align="center" prop="usedQuantity" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          min-width="200"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="data.type === '0'"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="
                () => {
                  model = {
                    show: true,
                    data: {
                      ...scope.row,
                    },
                  };
                }
              "
              v-hasPermi="['md:fabric:edit']"
              >算料模型</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['md:fabric:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['md:fabric:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改商品模型物料维护对话框 -->
      <el-dialog
        :close-on-click-modal="false"
        :title="title"
        :visible.sync="open"
        width="700px"
        append-to-body
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="110px"
          class="open-form"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="类型" prop="type">
                <el-select
                  v-model="form.type"
                  placeholder="请选择类型"
                  style="width: 100%"
                  @change="handleTypeChange"
                >
                  <el-option label="面料" value="0" />
                  <el-option label="辅料" value="1" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用料环节" prop="usageProcess">
                <el-select
                  v-model="form.usageProcess"
                  placeholder="请选择用料环节"
                  style="width: 100%"
                >
                  <template v-if="form.type === '0'">
                    <el-option label="下料" value="0" />
                  </template>
                  <template v-else-if="form.type === '1'">
                    <el-option label="加工" value="1" />
                    <el-option label="配货" value="2" />
                  </template>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="位置" prop="part">
                <el-input v-model="form.part" placeholder="请输入位置" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="是否自动拆单"
                prop="isAutomaticOrderSplitting"
              >
                <el-checkbox
                  v-model="form.isAutomaticOrderSplitting"
                  true-label="Y"
                  false-label="N"
                  >自动</el-checkbox
                >
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="宽度比例" prop="widthRatio">
                <el-input-number
                  style="width: 100%"
                  v-model="form.widthRatio"
                  placeholder="请输入宽度比例"
                  clearable
                  :controls="false"
                ></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="宽度附加" prop="widthAddition">
                <el-input-number
                  style="width: 100%"
                  v-model="form.widthAddition"
                  placeholder="请输入宽度附加"
                  clearable
                  :controls="false"
                ></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="高度比例" prop="heightRatio">
                <el-input-number
                  style="width: 100%"
                  v-model="form.heightRatio"
                  placeholder="请输入高度比例"
                  clearable
                  :controls="false"
                ></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="高度附加" prop="heightAddition">
                <el-input-number
                  style="width: 100%"
                  v-model="form.heightAddition"
                  placeholder="请输入高度附加"
                  clearable
                  :controls="false"
                ></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="每N件" prop="unitQuantity">
                <el-input
                  v-model="form.unitQuantity"
                  placeholder="请输入每N件"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="使用数量" prop="usedQuantity">
                <el-input
                  v-model="form.usedQuantity"
                  placeholder="请输入使用数量"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>

    <!-- 窗幔模型 -->
    <ModelView v-if="model.show" :show.sync="model.show" v-bind="model" />
  </el-drawer>
</template>

<script>
import {
  listProductModelFabric,
  getProductModelFabric,
  delProductModelFabric,
  addProductModelFabric,
  updateProductModelFabric,
} from "@/api/process/vm";
import { USAGE_PROCESS } from "../../configure";
import ModelView from "./model.vue";

export default {
  name: "ProductModelFabric",
  components: {
    ModelView,
  },
  props: {
    show: Boolean,
    type: {
      type: String,
      default: "add",
    },
    data: {
      type: Object,
    },
  },
  data() {
    return {
      USAGE_PROCESS,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品模型物料维护表格数据
      fabricList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        type: null,
        usageProcess: null,
        part: null,
        isAutomaticOrderSplitting: null,
        widthRatio: null,
        widthAddition: null,
        heightRatio: null,
        heightAddition: null,
        unitQuantity: null,
        usedQuantity: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        part: [
          {
            required: true,
            message: "请输入位置",
            trigger: "change",
          },
        ],
      },
      model: {
        show: false,
        data: null,
      },
    };
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  created() {
    this.getList();
    console.log(this.data);
  },
  methods: {
    /** 查询商品模型物料维护列表 */
    getList() {
      this.loading = true;
      listProductModelFabric({
        ...this.queryParams,
        productModelId: this.data.mdWindowCurtainModelId,
      }).then((response) => {
        this.fabricList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mdProductModelFabricId: null,
        type: "0",
        usageProcess: "0",
        part: null,
        isAutomaticOrderSplitting: "N",
        widthRatio: null,
        widthAddition: null,
        heightRatio: null,
        heightAddition: null,
        unitQuantity: null,
        usedQuantity: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.mdProductModelFabricId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品模型物料维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const mdProductModelFabricId = row.mdProductModelFabricId || this.ids;
      getProductModelFabric(mdProductModelFabricId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品模型物料维护";
      });
    },
    /** 选择类型 */
    handleTypeChange(val) {
      console.log(val);
      if (val === "0") {
        this.form.usageProcess = "0";
      } else if (val === "1") {
        this.form.usageProcess = "1";
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.productModelId = this.data.mdWindowCurtainModelId;
          if (this.form.mdProductModelFabricId != null) {
            updateProductModelFabric(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductModelFabric(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const mdProductModelFabricIds = row.mdProductModelFabricId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除商品模型物料维护编号为"' +
            mdProductModelFabricIds +
            '"的数据项？'
        )
        .then(function () {
          return delProductModelFabric(mdProductModelFabricIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "md/productModelFabric/export",
        {
          ...this.queryParams,
        },
        `productModelFabric_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.open-form {
  ::v-deep {
    .el-form-item {
      input {
        text-align: left;
      }
    }
  }
}
</style>
